package com.wonders.frame.chron.controller.flow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.frame.chron.config.ChronConfig;
import com.wonders.frame.chron.dao.card.CardDao;
import com.wonders.frame.chron.dao.summary.SummaryDao;
import com.wonders.frame.chron.dao.user.UserInfoDao;
import com.wonders.frame.chron.model.bo.UserTest;
import com.wonders.frame.chron.model.vo.UserInfo;
import com.wonders.frame.chron.service.CaRestAPIService;
import com.wonders.frame.chron.service.WebUtilService;
import com.wonders.frame.chron.service.summary.SummaryService;
import com.wonders.frame.chron.util.AddTodoItem;
import com.wonders.frame.chron.util.ConstantUtil;
import com.wonders.frame.chron.util.EnumConstant.UserGroup;
import com.wonders.frame.chron.util.LoginConstants;

@Controller
@RequestMapping("/flow")
public class FlowController {
	
	@Autowired
	WebUtilService webUtilService;
	@Autowired
	ChronConfig chronConfig;
	@Autowired
	CardDao cardDao;
	@Autowired
	SummaryService summaryService;
	@Autowired
	SummaryDao summaryDao;
	@Autowired
	UserInfoDao userInfoDao;
	
	@ResponseBody
    @RequestMapping(value = "/getUserGroup/{type}", method = RequestMethod.GET)
    public List<UserTest> getUserGroup(@PathVariable("type") UserGroup type,Model model, HttpServletRequest request, HttpServletResponse response) {
		List<UserTest> result = null;
		
		List<UserTest> list = null;
		UserTest u = null;
		
		switch(type){
			case DEPT_LEADER:
				list = new ArrayList<UserTest>();
				
				u = new UserTest();
				u.setId("G010000000722921");
				u.setUsername("朱效洁");
				list.add(u);
				
				result = list;
				
				break;
			case CHRON_LEADER:
				list = new ArrayList<UserTest>();
				
				u = new UserTest();
				u.setId("G020001000492549");
				u.setUsername("金涛");
				list.add(u);
				
				result = list;
				
				break;
			case CHRON_VIEW:
				list = new ArrayList<UserTest>();
				
				u = new UserTest();
				u.setId("G001000001702549");
				u.setUsername("郑思浩");
				list.add(u);
				
				result = list;
				
				break;
			case CHRON_REVIEW:
				list = new ArrayList<UserTest>();
				
				u = new UserTest();
				u.setId("G020001000492549");
				u.setUsername("金涛");
				list.add(u);
				
				result = list;
				
				break;
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/getUserInfoGroup/{type}", method = RequestMethod.GET)
    public List<UserInfo> getUserInfoGroup(@PathVariable("type") UserGroup type,Model model, HttpServletRequest request, HttpServletResponse response) {
		String deptId = (String)request.getSession().getAttribute(LoginConstants.DEPTID);
		//String userId = (String)request.getSession().getAttribute(LoginConstants.USERID);
		Boolean isChron = (Boolean)request.getSession().getAttribute(LoginConstants.IS_CHRON);
		String loginName=(String)request.getSession().getAttribute(LoginConstants.LOGINNAME);
		List<UserInfo> result = new ArrayList<UserInfo>();
		
		switch(type){
			case DEPT_LEADER:
				/*Set<String> userGroup = new HashSet<String>();
				userGroup.add(ConstantUtil.GROUP_DEPT_CHRON_LEADER);
				userGroup.add(ConstantUtil.GROUP_CHRON_USER);
				userGroup.add(ConstantUtil.GROUP_CHRON_LEADER);
				userGroup.add(ConstantUtil.GROUP_CHRON_VIEW_LEADER);
				userGroup.add(ConstantUtil.GROUP_CHRON_REVIEW_LEADER);*/
				
				//修志办领导
				//if(userInfoDao.findUserInGroup(userId,userGroup)){
				if(isChron){
					//result = userInfoDao.findByGroupNameAndDept(ConstantUtil.GROUP_DEPT_LEADER, deptId);
					
					result = userInfoDao.findByGroupName(ConstantUtil.GROUP_DEPT_CHRON_LEADER);
				}else{
					//判断当前登录人是否为需要指定领导用户(暂时)
					//填报人----直属领导
					UserInfo userInfo=new UserInfo();
					/*if ("TXYT000000012925".equals(loginName)) {//G040000000152925
						userInfo.setNAME("刘钧");
						userInfo.setLOGINORGANNAME("G040000000152925");
						result.add(userInfo);
					}else if("TBLW000000012925".equals(loginName)){//G010050004632925
						userInfo.setNAME("沈豪");
						userInfo.setLOGINORGANNAME("G010050004632925");
						result.add(userInfo);
					}else if("TWH0000000012925".equals(loginName)){//THDX000000012925
						userInfo.setNAME("黄丹雄");
						userInfo.setLOGINORGANNAME("THDX000000012925");
						result.add(userInfo);
					}else if("TWKY000000012925".equals(loginName)){//G020001000502925
						userInfo.setNAME("阮文");
						userInfo.setLOGINORGANNAME("G010070003422925");
						result.add(userInfo);
					}else if("G040000001512925".equals(loginName)){//G010050000042925
						userInfo.setNAME("朱小娟");
						userInfo.setLOGINORGANNAME("G010050000042925");
						result.add(userInfo);
					}else if("T010090001912668".equals(loginName)){//G010050000042925
						userInfo.setNAME("葛乃聪");
						userInfo.setLOGINORGANNAME("G010000000612664");
						result.add(userInfo);
					}else{*/
						HashMap<String,String> deptLeaders = CaRestAPIService.getDeptLeadersFromCa(request, response, deptId);
						for (Map.Entry<String, String> entry : deptLeaders.entrySet()) {
							userInfo = new UserInfo();
							userInfo.setNAME(entry.getValue());
							userInfo.setLOGINORGANNAME(entry.getKey());
							userInfo.setLOGINNAME(entry.getKey().substring(0,entry.getKey().length()-4));
							result.add(userInfo);
						}
						

						List<UserInfo> list = userInfoDao.findLeaderByUserName(loginName);
						result.addAll(list);
						
						if(loginName.endsWith("2920")){
							userInfo=new UserInfo();
							userInfo.setNAME("张乾睿");
							userInfo.setLOGINORGANNAME("G010040000412920");
							result.add(userInfo);
						}
//					}
					
					//result = userInfoDao.findByGroupNameAndDept(ConstantUtil.GROUP_DEPT_LEADER, deptId);
				}
				break;
			case CHRON_LEADER:
				result = userInfoDao.findByGroupName(ConstantUtil.GROUP_CHRON_LEADER);
				break;
			case CHRON_VIEW:
				result = userInfoDao.findByGroupName(ConstantUtil.GROUP_CHRON_VIEW_LEADER);
				break;
			case CHRON_REVIEW:
				result = userInfoDao.findByGroupName(ConstantUtil.GROUP_CHRON_REVIEW_LEADER);
				break;
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/flowCardStart", method = RequestMethod.POST)
    public Map<String, String> flowCardStart(Model model, HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> result = new HashMap<String, String>();
		
		String id = request.getParameter("id");
//		String createUserId = request.getParameter("createUserId");
		String createUserId = (String)request.getSession().getAttribute(LoginConstants.LOGINNAME);
		//String createUserName = request.getParameter("createUserName");
		String comment = request.getParameter("comment");
		String nextUserId = request.getParameter("nextUserId");
//		String type = request.getParameter("type");//用于判断是卡片管理流程还是长编汇总流程
		String contentTitle = request.getParameter("contentTitle");
		
		String flowSign = chronConfig.getCardFlowSign();
		String title = chronConfig.getCardTitle();
		String stepName = ConstantUtil.DEPT_LEADER;

		
		String dept="1";
		//判断该用户是否为修志办专员
		Boolean isChron=(Boolean) request.getSession().getAttribute(LoginConstants.IS_CHRON);
		if (isChron){
			stepName=ConstantUtil.CHRON_LEADER;
			dept="2";
		}			
		
		if("".equals(flowSign)){
			result.put("result", ConstantUtil.ERROR);
			return result;
		}
		
		try {
			String processInstanceId = webUtilService.startProcess(createUserId,comment,nextUserId,dept,flowSign,title);
			
			if(!ConstantUtil.ERROR.equals(processInstanceId)){
				
				String stepNameSecond = contentTitle+stepName;
				String processDefinitionId = webUtilService.getProcessDefinitionId(processInstanceId);
				
				//发送待办
				String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
				
				appUrl += chronConfig.getCardFlowNext()+id;
				
				String todoItemId=AddTodoItem.addTodoItem(chronConfig.getAddTaskApiUrl(), stepNameSecond, stepName, nextUserId, createUserId, appUrl, title);
				
				System.out.println("processInstanceId:"+processInstanceId);
				System.out.println("processDefinitionId:"+processDefinitionId);
				System.out.println("todoItemId:"+todoItemId);
				
				cardDao.updateCardById(todoItemId, processInstanceId, processDefinitionId, ConstantUtil.START, Integer.valueOf(id));
				
				result.put("result", ConstantUtil.SUCCESS);
				result.put("taskId", processInstanceId+ConstantUtil.FLOW_START);
			}else{
				result.put("result", ConstantUtil.ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", ConstantUtil.ERROR);
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/flowCardComplete", method = RequestMethod.POST)
    public String flowCardComplete(Model model, HttpServletRequest request, HttpServletResponse response) {
		String result;
		String id = request.getParameter("id");
		String createUserId = request.getParameter("createUserId");
		String nextUserId = request.getParameter("nextUserId");
		String comment = request.getParameter("comment");
		String flag = request.getParameter("flag"); //1：通过 0：不通过
		String todoItemId = request.getParameter("todoItemId");
		String processInstanceId = request.getParameter("processInstanceId");
		String taskId = request.getParameter("taskId");
//		String type = request.getParameter("type");//用于判断是卡片管理流程还是长编汇总流程
		String step = request.getParameter("step");//用于判断流程是否结束
		String contentTitle = request.getParameter("contentTitle");

		//消除待办
		AddTodoItem.confirmTodoItem(chronConfig.getUpdateTaskApiUrl(), todoItemId);
		
		try {
			String returnObj = webUtilService.complete(taskId, processInstanceId, comment, nextUserId, flag);
			
			if(!ConstantUtil.ERROR.equals(returnObj)){
				//结束流程或者取消申报不发待办(流程最后一个节点且审核结果为通过)
				if(ConstantUtil.STEP_END.equals(step) && ConstantUtil.PASS.equals(flag)){
					cardDao.updateCardById("", ConstantUtil.FINISH, Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}else if(ConstantUtil.STEP_STOP.equals(step) && ConstantUtil.NOT_PASS.equals(flag)){
					cardDao.updateCardById("", ConstantUtil.CANCLE, Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}else{
					//发送待办
					String title = "";
					String stepName = "";
					String stepNameSecond = "";
					String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
					Boolean flowFlag = false;//判断流程是否返回修改
					String status = "";
					
					title = chronConfig.getCardTitle();
					
					//返回修改后领导单位/部门领导审核
					if(ConstantUtil.STEP_DEPT_USER.equals(step)){

						Boolean isChron=(Boolean) request.getSession().getAttribute(LoginConstants.IS_CHRON);
						
						appUrl += chronConfig.getCardFlowNext()+id;
						
						stepName = ConstantUtil.DEPT_LEADER;
						if (isChron) 
							stepName=ConstantUtil.CHRON_LEADER;
						
						stepNameSecond = contentTitle+stepName;
					}
					//单位/部门领导审核
					if(ConstantUtil.STEP_DEPT_LEADER.equals(step)){
						if(ConstantUtil.PASS.equals(flag)){
							appUrl += chronConfig.getCardFlowNext()+id;
							stepName = ConstantUtil.CHRON_LEADER;
							stepNameSecond = contentTitle+stepName;
						}
						//退回流程(先获取流程发起人)
						if(ConstantUtil.NOT_PASS.equals(flag)){
							Map<String, String> keyMap = webUtilService.getStatus(processInstanceId);
							nextUserId = keyMap.get(ConstantUtil.LOGINNAME);
							appUrl += chronConfig.getCardFlowBack()+id;
							stepName = ConstantUtil.DEPT_LEADER+ConstantUtil.CHRON_BACK;
							stepNameSecond = contentTitle+stepName;
							
							flowFlag = true;
						}
					}
					//修志办领导审核
					if(ConstantUtil.STEP_END.equals(step)){
						//退回流程(先获取流程发起人)
						if(ConstantUtil.NOT_PASS.equals(flag)){
							Map<String, String> keyMap = webUtilService.getStatus(processInstanceId);
							nextUserId = keyMap.get(ConstantUtil.LOGINNAME);
							appUrl += chronConfig.getCardFlowBack()+id;
							stepName = ConstantUtil.CHRON_LEADER+ConstantUtil.CHRON_BACK;
							stepNameSecond = contentTitle+stepName;
							
							flowFlag = true;
						}
					}
					
					String todoItemIdNext=AddTodoItem.addTodoItem(chronConfig.getAddTaskApiUrl(), stepNameSecond, stepName, nextUserId, createUserId, appUrl, title);
					
					if(flowFlag){
						status = ConstantUtil.MODIFY;
					}else{
						status = ConstantUtil.PROGRESS;
					}
					cardDao.updateCardById(todoItemIdNext, status,Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}
			}else{
				result = ConstantUtil.ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantUtil.ERROR;
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/flowSummaryStart", method = RequestMethod.POST)
    public Map<String, String> flowSummaryStart(Model model, HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> result = new HashMap<String, String>();
		String id = request.getParameter("id");
//		String createUserId = request.getParameter("createUserId");
		String createUserId = (String)request.getSession().getAttribute(LoginConstants.LOGINNAME);
		//String createUserName = request.getParameter("createUserName");
		String comment = request.getParameter("comment");
		String nextUserId = request.getParameter("nextUserId");
//		String type = request.getParameter("type");//用于判断是卡片管理流程还是长编汇总流程
		String dept = request.getParameter("dept");//用于长编汇总流程判断 单位修志/修志办修志标识
		String contentTitle = request.getParameter("contentTitle");
		
		String flowSign = chronConfig.getSummaryFlowSign();
		String title = chronConfig.getSummaryTitle();

		if("".equals(flowSign)){
			result.put("result", ConstantUtil.ERROR);
			return result;
		}
		
		try {
			String processInstanceId= webUtilService.startSummaryProcess(createUserId,comment,nextUserId,dept,flowSign,title);
			
			if(!ConstantUtil.ERROR.equals(processInstanceId)){
				String stepName = "";
				String stepNameSecond = "";
				if(ConstantUtil.DEPT_1.equals(dept)){
					stepName = ConstantUtil.DEPT_LEADER;
					stepNameSecond = contentTitle+stepName;
				}
				if(ConstantUtil.DEPT_2.equals(dept)){
					stepName = ConstantUtil.CHRON_VIEW;
					stepNameSecond = contentTitle+stepName;
				}
				String processDefinitionId = webUtilService.getProcessDefinitionId(processInstanceId);
				
				//发送待办
				String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
				
				appUrl += chronConfig.getSummaryFlowNext()+id;
				
				String todoItemId=AddTodoItem.addTodoItem(chronConfig.getAddTaskApiUrl(), stepNameSecond, stepName, nextUserId, createUserId, appUrl, title);
				
				System.out.println("processInstanceId:"+processInstanceId);
				System.out.println("processDefinitionId:"+processDefinitionId);
				System.out.println("todoItemId:"+todoItemId);
				
				summaryDao.updateSummaryById(todoItemId, processInstanceId, processDefinitionId, ConstantUtil.START, Integer.valueOf(id));
				
				result.put("result", ConstantUtil.SUCCESS);
				result.put("taskId", processInstanceId+ConstantUtil.FLOW_START);
			}else{
				result.put("result", ConstantUtil.ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", ConstantUtil.ERROR);
		}
		
		return result;
    }
	
	
	
	@ResponseBody
    @RequestMapping(value = "/flowSummaryComplieComplete", method = RequestMethod.POST)
    public String flowSummaryComplieComplete(Model model, HttpServletRequest request, HttpServletResponse response) {
		String result;
		String id = request.getParameter("id");
		String createUserId = request.getParameter("createUserId");
		String nextUserId = request.getParameter("nextUserId");
		String comment = request.getParameter("comment");
		String flag = request.getParameter("flag"); //1：通过 0：不通过
		String todoItemId = request.getParameter("todoItemId");
		String processInstanceId = request.getParameter("processInstanceId");
		String taskId = request.getParameter("taskId");
//		String type = request.getParameter("type");//用于判断是卡片管理流程还是长编汇总流程
		String step = request.getParameter("step");//用于判断流程是否结束
		String dept = request.getParameter("dept");//用于长编汇总流程判断 单位修志/修志办修志标识
		String contentTitle = request.getParameter("contentTitle");
		String complieStatus =  request.getParameter("complieStatus");
		
		//消除待办
		AddTodoItem.confirmTodoItem(chronConfig.getUpdateTaskApiUrl(), todoItemId);
		
		try {
			if(complieStatus.equals(ConstantUtil.MODIFY)){
				summaryDao.updateSummaryComplieById(ConstantUtil.START, Integer.parseInt(id));
			}else if(complieStatus.equals(ConstantUtil.START)) {
				if(flag.equals("1")){
					summaryDao.updateSummaryComplieById(ConstantUtil.FINISH, Integer.parseInt(id));
				}else {
					summaryDao.updateSummaryComplieById(ConstantUtil.MODIFY, Integer.parseInt(id));
				}
			}
			
			
			result = ConstantUtil.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantUtil.ERROR;
		}
		
		return result;
    }
	
	
	
	@ResponseBody
    @RequestMapping(value = "/flowSummaryComplete", method = RequestMethod.POST)
    public String flowSummaryComplete(Model model, HttpServletRequest request, HttpServletResponse response) {
		String result;
		String id = request.getParameter("id");
		String createUserId = request.getParameter("createUserId");
		String nextUserId = request.getParameter("nextUserId");
		String comment = request.getParameter("comment");
		String flag = request.getParameter("flag"); //1：通过 0：不通过
		String todoItemId = request.getParameter("todoItemId");
		String processInstanceId = request.getParameter("processInstanceId");
		String taskId = request.getParameter("taskId");
//		String type = request.getParameter("type");//用于判断是卡片管理流程还是长编汇总流程
		String step = request.getParameter("step");//用于判断流程是否结束
		String dept = request.getParameter("dept");//用于长编汇总流程判断 单位修志/修志办修志标识
		String contentTitle = request.getParameter("contentTitle");
		
		//消除待办
		AddTodoItem.confirmTodoItem(chronConfig.getUpdateTaskApiUrl(), todoItemId);
		
		try {
			String returnObj = webUtilService.summaryComplete(taskId, processInstanceId, comment, nextUserId, dept, flag);
			
			if(!ConstantUtil.ERROR.equals(returnObj)){
				//结束流程不发待办(流程最后一个节点且审核结果为通过)
				if(ConstantUtil.STEP_END.equals(step) && ConstantUtil.PASS.equals(flag)){
					summaryDao.updateSummaryById("", ConstantUtil.FINISH, Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}else if(ConstantUtil.STEP_STOP.equals(step) && ConstantUtil.NOT_PASS.equals(flag)){
					summaryDao.updateSummaryById("", ConstantUtil.CANCLE, Integer.valueOf(id));
					
					summaryService.delete(Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}else{
					//发送待办
					String title = "";
					String stepName = "";
					String stepNameSecond = "";
					String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
					Boolean flowFlag = false;//判断流程是否返回修改
					String status = "";
					
					title = chronConfig.getSummaryTitle();
					//返回修改后领导单位/部门领导审核
					if(ConstantUtil.STEP_DEPT_USER.equals(step)){
						appUrl += chronConfig.getSummaryFlowNext()+id;
						if(ConstantUtil.DEPT_1.equals(dept)){
							stepName = ConstantUtil.DEPT_LEADER;
							stepNameSecond = contentTitle+stepName;
						}
						if(ConstantUtil.DEPT_2.equals(dept)){
							stepName = ConstantUtil.CHRON_VIEW;
							stepNameSecond = contentTitle+stepName;
						}
					}
					//单位/部门领导审核
					if(ConstantUtil.STEP_DEPT_LEADER.equals(step)){
						if(ConstantUtil.PASS.equals(flag)){
							appUrl += chronConfig.getSummaryFlowNext()+id;
							stepName = ConstantUtil.CHRON_VIEW;
							stepNameSecond = contentTitle+stepName;
						}
						//退回流程(先获取流程发起人)
						if(ConstantUtil.NOT_PASS.equals(flag)){
							Map<String, String> keyMap = webUtilService.getStatus(processInstanceId);
							nextUserId = keyMap.get(ConstantUtil.LOGINNAME);
							appUrl += chronConfig.getSummaryFlowBack()+id;
							stepName = ConstantUtil.DEPT_LEADER+ConstantUtil.CHRON_BACK;
							stepNameSecond = contentTitle+stepName;
							
							flowFlag = true;
						}
					}
					//修志办领导初审
					if(ConstantUtil.STEP_CHRON_VIEW.equals(step)){
						if(ConstantUtil.PASS.equals(flag)){
							appUrl += chronConfig.getSummaryFlowNext()+id;
							stepName = ConstantUtil.CHRON_REVIEW;
							stepNameSecond = contentTitle+stepName;
						}
						//退回流程(先获取流程发起人)
						if(ConstantUtil.NOT_PASS.equals(flag)){
							Map<String, String> keyMap = webUtilService.getStatus(processInstanceId);
							nextUserId = keyMap.get(ConstantUtil.LOGINNAME);
							appUrl += chronConfig.getSummaryFlowBack()+id;
							stepName = ConstantUtil.CHRON_VIEW+ConstantUtil.CHRON_BACK;
							stepNameSecond = contentTitle+stepName;
							
							flowFlag = true;
						}
					}
					//修志办领导复审
					if(ConstantUtil.STEP_END.equals(step)){
						//退回流程(先获取流程发起人)
						if(ConstantUtil.NOT_PASS.equals(flag)){
							Map<String, String> keyMap = webUtilService.getStatus(processInstanceId);
							nextUserId = keyMap.get(ConstantUtil.LOGINNAME);
							appUrl += chronConfig.getSummaryFlowBack()+id;
							stepName = ConstantUtil.CHRON_REVIEW+ConstantUtil.CHRON_BACK;
							stepNameSecond = contentTitle+stepName;
							
							flowFlag = true;
						}
					}
					
					String todoItemIdNext=AddTodoItem.addTodoItem(chronConfig.getAddTaskApiUrl(), stepNameSecond, stepName, nextUserId, createUserId, appUrl, title);
					
					if(flowFlag){
						status = ConstantUtil.MODIFY;
					}else{
						status = ConstantUtil.PROGRESS;
					}
					summaryDao.updateSummaryById(todoItemIdNext, status, Integer.valueOf(id));
					
					result = ConstantUtil.SUCCESS;
				}
			}else{
				result = ConstantUtil.ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantUtil.ERROR;
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/getStatus", method = RequestMethod.POST)
    public Map<String, String> getStatus(Model model, HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> result = new HashMap<String, String>();
		String processInstanceId = request.getParameter("processInstanceId");
		
		try {
			Map<String, String> taskMap = webUtilService.getStatus(processInstanceId);
			if(taskMap != null){
				String statu = taskMap.get("statu");
				String loginName = taskMap.get("loginName");
				String taskId = taskMap.get("taskId");

				result.put("result", ConstantUtil.SUCCESS);
				result.put("statu", statu);
				result.put("loginName", loginName);
				result.put("taskId", taskId);
			}else{
				result.put("result", ConstantUtil.ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", ConstantUtil.ERROR);
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/getComments", method = RequestMethod.POST)
    public List<Map<String,String>> getComments(Model model, HttpServletRequest request, HttpServletResponse response) {
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		String processInstanceId = request.getParameter("processInstanceId");
		
		Set<String> userSet = new HashSet<String>();
		
		try {
			result = webUtilService.getComments(processInstanceId);
			
			for (Map<String, String> map : result) {
				String userId = map.get("userId");
				if(StringUtils.isNotEmpty(userId)){
					userSet.add(userId);
				}
			}
			
			Map<String,String> resultMap = new HashMap<String, String>();
			
			if(userSet.size() > 0){
				resultMap = userInfoDao.findByIds(userSet);
				
				if(resultMap != null && !resultMap.isEmpty()){
					for (Map<String, String> map : result) {
						map.put("username",resultMap.get(map.get("userId")));
						map.put("deptName",resultMap.get(map.get("userId")+"deptName"));
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
    }
	
	@ResponseBody
    @RequestMapping(value = "/isChronUser/{userId}", method = RequestMethod.GET)
    public Map<String,Object> isChronUser(@PathVariable("userId") String userId,Model model, HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> result = new HashMap<String,Object>();
		
		Set<String> userGroup = new HashSet<String>();
		userGroup.add(ConstantUtil.GROUP_DEPT_CHRON_LEADER);
		userGroup.add(ConstantUtil.GROUP_CHRON_USER);
		userGroup.add(ConstantUtil.GROUP_CHRON_LEADER);
		userGroup.add(ConstantUtil.GROUP_CHRON_VIEW_LEADER);
		userGroup.add(ConstantUtil.GROUP_CHRON_REVIEW_LEADER);
		
		try {
			boolean flag = userInfoDao.findUserInGroup(userId, userGroup);
			if(flag){
				result.put("result", ConstantUtil.SUCCESS);
				result.put("flag", flag);
			}else{
				result.put("result", ConstantUtil.ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", ConstantUtil.ERROR);
		}
		
		return result;
    }
}
